package org.example.school.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.example.school.common.Context;
import org.example.school.common.Result;
import org.example.school.entity.EventRegistrations;
import org.example.school.entity.SchoolEvents;
import org.example.school.model.dto.EventsDTO;
import org.example.school.service.IEventRegistrationsService;
import org.example.school.service.ISchoolEventsService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 管理员控制器
 * 处理管理员相关操作，如校园活动管理等
 */
@RestController
@RequestMapping("/admin")
@CrossOrigin
public class AdminController {

    @Autowired
    private ISchoolEventsService schoolEventsService;
    
    @Autowired
    private IEventRegistrationsService eventRegistrationsService;

    /**
     * 发布新活动
     * @param eventsDTO 活动信息
     * @return 发布结果
     */
    @PostMapping("/events")
    public Result<String> createEvent(@RequestBody EventsDTO eventsDTO) {
        try {
            SchoolEvents schoolEvents = new SchoolEvents();
            BeanUtils.copyProperties(eventsDTO, schoolEvents);
            // 设置创建时间
            schoolEvents.setCreatedAt(LocalDateTime.now());
            // 设置发布者ID（当前登录用户）
            schoolEvents.setPublisherId(Context.getId());
            // 设置学校ID（这里暂时设置为1，实际应该从用户信息中获取）
            schoolEvents.setSchoolId(1L);
            schoolEventsService.save(schoolEvents);
            return Result.success("活动发布成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("活动发布失败: " + e.getMessage());
        }
    }

    /**
     * 获取所有活动
     * @return 活动列表
     */
    @GetMapping("/events")
    public Result<List<SchoolEvents>> getEvents() {
        try {
            List<SchoolEvents> events = schoolEventsService.list(new QueryWrapper<SchoolEvents>().orderByDesc("created_at"));
            return Result.success(events);
        } catch (Exception e) {
            return Result.error("获取活动列表失败: " + e.getMessage());
        }
    }

    /**
     * 删除活动
     * @param id 活动ID
     * @return 删除结果
     */
    @DeleteMapping("/events/{id}")
    public Result<String> deleteEvent(@PathVariable Long id) {
        try {
            schoolEventsService.removeById(id);
            return Result.success("活动删除成功");
        } catch (Exception e) {
            return Result.error("活动删除失败: " + e.getMessage());
        }
    }
    
    /**
     * 获取活动报名情况
     * @param eventId 活动ID
     * @return 报名列表
     */
    @GetMapping("/events/{eventId}/registrations")
    public Result<List<EventRegistrations>> getEventRegistrations(@PathVariable Long eventId) {
        try {
            List<EventRegistrations> registrations = eventRegistrationsService.list(
                new QueryWrapper<EventRegistrations>().eq("event_id", eventId)
            );
            return Result.success(registrations);
        } catch (Exception e) {
            return Result.error("获取报名列表失败: " + e.getMessage());
        }
    }
}